<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录" />
    <!-- 表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        required
        clearable
        v-model="user.mobile"
        name="mobile"
        :rules="userFormRules.mobile"
        placeholder="请输入手机号"
        autocomplete="off"
      >
        <template #left-icon>
          <i class="toutiao toutiao-shouji"></i>
        </template>
      </van-field>
      <van-field
        required
        clearable
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        autocomplete="off"
      >
        <template #left-icon>
          <i class="toutiao toutiao-yanzhengma"></i>
        </template>
        <template #button>
          <van-count-down
            v-if="isCountDownShow"
            :time="1000 * 5"
            format="ss s"
            @finish="isCountDownShow = false"
          />
          <van-button
            v-else
            @click="onSendSms"
            native-type="button"
            class="send-sms-btn"
            round
            size="small"
            type="primary"
            >验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, getSmsCode } from '@/api/user.js'
export default {
  name: 'LoginPage',
  data() {
    return {
      user: {
        // code: '',
        mobile: '13911111111',
        code: '246810',
      },
      userFormRules: {
        mobile: [
          // 没有值 : 校验不能为空
          { required: true, message: '手机号不能为空' },
          // 有值 : 校验格式正不正确
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: '手机号格式不正确',
          },
        ],
        code: [
          // 没有值 : 校验不能为空
          { required: true, message: '验证码不能为空' },
          // 有值 : 校验格式正不正确
          { pattern: /^\d{6}$/, message: '验证码格式不正确' },
        ],
      },
      isCountDownShow: false, // 倒计时是否显示
    }
  },
  methods: {
    // 点击发送验证码
    async onSendSms() {
      //1. 手动对手机号进行验证, 因为发送验证码就是应该发送给一个正确的手机号
      try {
        await this.$refs.loginForm.validate('mobile')
        console.log('手机号校验正确的')
      } catch (error) {
        return console.log('手机号校验xxxx的')
      }

      //2. 倒计时开始
      this.isCountDownShow = true

      //3. 发送验证码请求
      // let res =  getSmsCode(this.user.mobile)      // 104 行 => res 得到的 getSmsCode() 的返回值 return promise
      try {
        await getSmsCode(this.user.mobile) // 105 行 => res 得到的 getSmsCode() 的请求的结果值 .then(res => )
        this.$toast.success('成功')
      } catch (error) {
        this.isCountDownShow = false

        if (error.response.status === 429) {
          this.$toast.fail('发送太频繁了')
        } else {
          this.$toast.fail('发送失败')
        }
      }
    },
    // 点击登录
    async onSubmit() {
      console.log('我被点击了')
      //1. 首先先弹出一个加载框
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
      })

      try {
        let res = await login(this.user)
        console.log(res.data.data)
        this.$toast.success('登录成功')
        this.$router.push('/layout/my')
        this.$store.commit('setUser', res.data.data)
      } catch (error) {
        console.log('失败')
        this.$toast.fail('登录失败')
      }
    },
  },
}
</script>
<style scoped lang="less">
.login-container {
  .navbar {
    background: #5094f3;
  }
  .van-nav-bar__title {
    color: #fff;
  }
}

.login-container {
  .toutiao {
    font-size: 20px;
  }
  .send-sms-btn {
    width: 142px;
    height: 36px;
    line-height: 36px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
